using System;
using System.Collections.Generic;
using System.Text;

namespace EuropeEngulfed.NET
{
    class GroundSupportUnit : PlayingPiece
    {
        public GroundSupportUnit(MapInfo.CountryID countryOwner) : base(countryOwner)
        {
            OutOfSupplyState = OOSState.InSupply;
        }

        public GroundSupportUnit(GroundSupportUnit GSUToCopy) : base(GSUToCopy.countryOwner)
        {
            this.OutOfSupplyState = GSUToCopy.OutOfSupplyState;
        }


        public override bool Equals(PlayingPiece playingPiece)
        {
            if (playingPiece is GroundSupportUnit)
                if (playingPiece.CountryOwner == this.CountryOwner)
                    if(playingPiece.OutOfSupply == this.OutOfSupply)
                        return true;
            return false;
        }

        public new GroundSupportUnit Copy()
        {
            GroundSupportUnit GSUCopy = new GroundSupportUnit(this.CountryOwner);
            GSUCopy.OutOfSupplyState = this.OutOfSupplyState;
            return GSUCopy;
        }

        public override string GetDescription()
        {
            return "GSU";
        }

        public System.Xml.XmlNode Serialise(System.Xml.XmlDocument xml)
        {
            System.Xml.XmlNode nodeToReturn = xml.CreateElement("GSU");
            System.Xml.XmlAttribute countryAttr = xml.CreateAttribute("Country");
            countryAttr.Value = Enum.GetName(typeof(MapInfo.CountryID), this.CountryOwner);
            nodeToReturn.Attributes.Append(countryAttr);
            System.Xml.XmlNode OOSNode = xml.CreateElement("OOS");
            OOSNode.InnerText = this.OutOfSupply.ToString();
            nodeToReturn.AppendChild(OOSNode);
            return nodeToReturn;
        }

        public new static GroundSupportUnit Deserialise(System.Xml.XmlNode node)
        {
            GroundSupportUnit GSUToReturn = null;
            if(node.Name == "GSU")
            {
                GSUToReturn = new GroundSupportUnit((MapInfo.CountryID)Enum.Parse(typeof(MapInfo.CountryID), node.Attributes["Country"].Value));
                foreach(System.Xml.XmlNode GSUNode in node)
                {
                    switch(GSUNode.Name)
                    {
                        case "OOS":
                            try
                            {
                                GSUToReturn.OutOfSupplyState = (OOSState)Enum.Parse(typeof(OOSState), GSUNode.InnerText);
                            }
                            catch //TO allow me to load old files that had just true/false for the supply state. /TODO remove
                            {
                                bool bOOS = bool.Parse(GSUNode.InnerText);
                                if (bOOS)
                                    GSUToReturn.OutOfSupplyState = OOSState.OOS;
                                else
                                    GSUToReturn.OutOfSupplyState = OOSState.InSupply;
                            }
                            break;
                    }
                }
            }

            return GSUToReturn;
        }

        public bool Equals(GroundSupportUnit GSUToCompare)
        {
            if (this.countryOwner != GSUToCompare.countryOwner)
                return false;
            if (this.OutOfSupply != GSUToCompare.OutOfSupply)
                return false;
            return true;
        }

        public override bool CanDoDoubleMovement()
        {
            return true;
        }

        public override string GetHiddenStringDescription()
        {
            return MapInfo.GetCountryAdjective(this.countryOwner) + " GSU";
        }
    }
}
